import axios from 'axios';
import { Dialog } from '@alifd/next';

axios.interceptors.request.use((config) => { // 这里的config包含每次请求的内容
  config.baseURL = localStorage.getItem('serveHost');
  config.headers.authorization = localStorage.getItem('authorization') || '';
  config.headers.lrzAdminHeader = 'lrzfusiondesign';
  return config;
}, (error) => {
  console.log('token_invalidate.err', error);
  return Promise.reject(error);
});

axios.interceptors.response.use((response) => {
  const res = response.data;
  if (res.errorCode === 'token_invalidate') {
    // clearAuthToken()
  }
  console.log('response:', response);
  if (res.code !== 200) {
    console.log('res', res);
    if (res.code === 401) {
      localStorage.clear();
      Dialog.alert({
        title: '提示',
        content: '登录已失效，请重新登录',
        onOk: () => window.location.replace('#/user/login'), // = '#/user/login',
      });
    }
  }
  return res;
}, (error) => {
  console.log('token_invalidate.err1', error);
  return Promise.reject(error);
});

export default axios;
